<template>
  <div>
    <criteria-paged :model="model" v-ref:paged>
      <criteria partial='criteria' @condition-changed='search' v-ref:criteria>
        <validator name='v' partial>
        <div  class="form-inline auto" >
          <div class="form-group">
            <div class="form-group" >
              <label for="f_subcompany">分公司：</label>
              <v-select id="f_subcompany"
              placeholder='请选择分公司'
              :value.sync="$parent.$parent.model.f_subcompany"
              :options='$parent.$parent.f_divisions'
              close-on-select clear-button></v-select>
            </div>
            <div class="form-group">
              <label for="f_check_type">用户类型：</label>
              <v-select id="f_check_type"
              placeholder='请选择用户类型'
              :value.sync="$parent.$parent.model.f_check_type"
              :options='$parent.$parent.user_types'
              close-on-select clear-button></v-select></v-select>
            </div>
            <div class="form-group">
              安检时间 从:
              <datepicker id="startdate"
                v-model='model.date1'
                :value.sync="model.date1"
                :disabled-days-of-Week="[]"
                :format="'yyyy-MM-dd'"
                :show-reset-button="reset">
              </datepicker>
            </div>
            <div class="form-group">
              到:
              <datepicker id="startdate"
              v-model='model.date2'
              :value.sync="model.date2"
                :disabled-days-of-Week="[]"
                :format="'yyyy-MM-dd'"
                :show-reset-button="reset">
              </datepicker>
            </div>
            <div class="form-group">
              安检计划:
              <input type="text" class="form-control" v-model="$parent.$parent.model.f_check_plan.f_plan_name" @click="$parent.$parent.showChooser"  readonly="readonly">
            </div>
            <div class="form-group">
              <label for="f_check_type">隐患类型：</label>
              <v-select id="f_check_type"
              placeholder='请选择隐患类型'
              :value.sync="$parent.$parent.model.f_defect"
              :options='$parent.$parent.defects_option'
              close-on-select clear-button></v-select></v-select>
            </div>
            <div class="form-group">
              <button class="btn btn-success width-80" @click="search()">查询</button>
            </div>
            <div class="form-group">
              <modal :show.sync="$parent.$parent.showModal" v-ref:modal>
                <div slot="modal-header" class="modal-header">
                  <h4 class="modal-title">
                    选择计划
                  </h4>
                </div>
                <div slot="modal-body" class="modal-body">
                  <plan-chooser :f.sync="$parent.$parent.f"  v-ref:chooser></plan-chooser>
                </div>
                <div slot="modal-footer" class="modal-footer">
                  <button type="button" class="btn btn-success width-80" @click="$parent.$parent.ok">确认</button>
                  <button type="button" class="btn btn-default" @click="$parent.$parent.cancel">取消</button>
                </div>
              <modal>
            </div>
        </div>
        </validator>
      </criteria>
      <data-grid :model="model" partial='list' v-ref:grid>
        <template partial='head'>
          <tr>
            <th>安检单</th>
            <th>用户姓名</th>
            <th>用户地址</th>
            <th>安检日期</th>
            <th>设备类型</th>
            <th>隐患级别</th>
            <th>隐患描述</th>
          </tr>
        </template>
        <template partial='body'>
          <td><button class="btn btn-default"  @click='$parent.$parent.$parent.change(row)'>查看</button></td>
          <td>{{row.f_user_name}}</td>
          <td>{{row.f_address}}</td>
          <td>{{row.f_offsite_time}}</td>
          <td>{{row.f_device_type}}</td>
          <td>{{row.f_defect_level}}</td>
          <td>{{row.f_defect}}</td>
        </template>
      </data-grid>
    </criteria-paged>
  </div>
</template>

<script>
import { PagedList } from 'vue-client'
import Vue from 'vue'
import AppData from '../../stores/AppData'

export default {
  title: '隐患查询',
  props: ['f'],
  data () {
    let model = new PagedList('rs/sql/隐患明细查询', 20,
      {date1: 'this.model.date1', date2: 'this.model.date2',
      f_defect_level: 'this.f_defect_', f_subcompany: 'this.f_subcompany[0]',
      f_check_type: 'this.f_check_type[0]', f_plan_id: 'this.f_check_plan.id'})
    model.f_subcompany = [Vue.f_subcompany]
    model.f_check_type = ['']
    model.date1 = ''
    model.date2 = ''
    model.f_check_plan = {f_plan_name: '', id: ''}
    model.f_defect = ['']
    model.f_defect_ = ''
    return {
      model: model,
      // f_divisions: Vue.f_subcompany != '' ? [{label: Vue.f_subcompany, value: Vue.f_subcompany}]: this.$getParams('安检系统用子公司', [{label:'请选择分公司',value:''}]),
      // user_types: this.$getParams('安检用户类型', [{label:'请选择用户类型',value:''}]),
      showModal: false
      // defects_option: this.$getParams('隐患级别', [{label:'请选择隐患级别',value:''}])
    }
  },
  watch: {
    'model.f_defect' (val) {
        if(val[0].length == 0)
          return
        if(val[0].length == 1)
          this.model.f_defect_ = 'like \'' + val[0] + '%\''
      else
          this.model.f_defect_ = ' = \'' + val[0].substring(0, val[0].indexOf('-')) + '\''
    }
  },
  computed: {
    defects_option () {
      return [{label: '全部', value: ''}, ...AppData.getParam('隐患类型')]
    },
    f_divisions () {
      return [{label: '全部', value: ''}, ...AppData.getParam('分公司')]
    },
    user_types () {
      return [{label: '全部', value: ''}, ...AppData.getParam('用户类型')]
    }
  },
  methods: {
    showChooser () {
      this.showModal = true
    },
    ok () {
      this.showModal = false
      if(this.$refs.paged.$refs.criteria.$refs.modal.$children[0].selectedRow.f_plan_id)
        this.model.f_check_plan = this.$refs.paged.$refs.criteria.$refs.modal.$children[0].selectedRow
    },
    cancel () {
      this.showModal = false
      this.model.f_check_plan = {f_plan_name: '', id: ''}
    },
    change (row) {
      if(row.f_check_type == '居民') {
        this.$goto('check-paper', {f_plan_id: row.f_check_plan_id, item: row, role: 'view'}, 'self')
      } else {
        this.$goto('check-paper-noncivil', {f_plan_id: row.f_check_plan_id, item: row, role: 'view'}, 'self')
      }
    }
  }
}
</script>
